package com.zhangyuji.common;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.util.Random;

public class SendMsgUtil {
    private static String HOST_NAME = "smtp.qq.com";
    private static String AUTHENTICATION = "941608267@qq.com";
    private static String ACCESS_CODE = "lvewjkrbgcgtbfaa";
    private static String SEND_EMAIL = "941608267@qq.com";
    private static String SEND_NAME = "zhang大帅";
    private static String EMAIL_SUBJECT ="验证码";
    private static String SEND_MESSAGE = "验证码为";





    public static void sendMsg(String targetEmail,String code){
        try {
            SimpleEmail simpleEmail = new SimpleEmail();
            // 发送邮件的服务器
            simpleEmail.setHostName(HOST_NAME);
            // 刚刚记录的授权码，是开启SMTP的密码
            simpleEmail.setAuthentication(AUTHENTICATION,ACCESS_CODE);
            // 发送邮件的邮箱和发件人
            simpleEmail.setFrom(SEND_EMAIL,SEND_NAME);
            // 使用安全链接
            simpleEmail.setSSLOnConnect(true);
            // 接收的邮箱
            simpleEmail.addTo(targetEmail);
            // 邮件的主题
            simpleEmail.setSubject(EMAIL_SUBJECT);
            // 邮件的内容
            simpleEmail.setMsg(SEND_MESSAGE +":["+ code + "],请妥善保管!");
            // 发送
            simpleEmail.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
    public static String generatRandom(int num){
        int[] code = new int[num];

        String codeNum = "";

        Random random = new Random();

        for (int i = 0; i < num; i++) {
            code[i] = random.nextInt(10);
            System.out.println(code[i]);
        }

        for (int i = 0; i < code.length; i++) {
            codeNum += code[i] + "";
        }
        System.out.println(codeNum);

        return codeNum;
    }
}
